<template>
	<view>
		<myhead txt="首页"></myhead>
		<view class="nav">
			<view :class="state==1?'state':''" data-state="1" @tap="stateBtn">我要买</view>
			<view :class="state==2?'state':''" data-state="2" @tap="stateBtn">我要卖</view>
		</view>
		<view class="list" v-for="(item,index) in list" :key="index">
			<view>
				<view>{{item.buyType==0?'出售':'购买'}}{{item.type}}<span>{{item.nickName}}</span></view>
				<view class="czBtn">
					990 | 95%
				</view>
			</view>
			<view>
				<view>数量：{{item.pnumber}} USDT</view>
				<view>单价</view>
			</view>
			<view>
				<view>限额：￥{{item.minAmount}}-￥{{item.maxAmount}}</view>
				<view class="num"><span>￥</span>{{item.amount}}</view>
			</view>
			<span class="gmBtn1" @tap="gmBtn" :data-id="item.id"><span class="gmBtn">购买</span></span>
		</view>
		<!-- <view class="list">
			<view>
				<view>购买USDT<span>昵称</span></view>
				<view class="czBtn">
					990 | 98%
				</view>
			</view>
			<view>
				<view>数量：4.66323521554 USDT</view>
				<view>单价</view>
			</view>
			<view>
				<view>限额：￥100.00-￥200.00</view>
				<view class="num"><span>￥</span>100.00</view>
			</view>
			<span class="gmBtn1"><span class="gmBtn">购买</span></span>
		</view> -->
		<block v-if="xdBol && gmInfo">
			<view class="xd">
				<view class="xdMain">
					<view>
						<view>购买USDT</view>
						<view>单价￥{{gmInfo.amount}}</view>
					</view>
					<view>
						<view class="xdNav">
							<view :class="xdNavC==1?'xdNavC':''" data-state="1" @tap="xdNavBtn">按金额购买</view>
							<view :class="xdNavC==2?'xdNavC':''" data-state="2" @tap="xdNavBtn">按数量购买</view>
						</view>
						<view class="xdInp">
							<input type="text" :placeholder="xdNavC==1?'请输入购买金额,单位:CNY':'请输入购买数量'" value="num" v-model="num"/>
							<view @tap="qbBtn">全部买入</view>
						</view>
						<view class="xdTxt">限额：￥{{gmInfo.minAmount}}-￥{{gmInfo.maxAmount}}</view>
						<view class="xdTxt">交易数量：{{gmInfo.pnumber}} USDT</view>
						<view class="xdTxt">实付款：<span><span>￥</span>{{num?xdNavC==1?num:(num*gmInfo.amount).toFixed(2):0}}</span></view>
						<view class="btnList">
							<view @tap="qxBtn">取消</view>
							<view @tap="orderBtn">下单</view>
						</view>
					</view>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	const http = require('../../utils/http.js');
	export default {
		data() {
			return {
				state:1,
				xdNavC:1,
				xdBol:false,
				list:null,
				gmInfo:null,
				num:null,
				num1:null
			}
		},
		onShow() {
			uni.showLoading({
			    title: '加载中'
			});
			http.get('/api/purchase/productList',{buyType:this.state==1?0:1}).then(res => {
				if (res.code == 200 && res.result) {
					this.list=res.result
					uni.hideLoading();
					if(!res.result.length){
						uni.showToast({
						  title: '暂无数据',
						  icon: 'none'
						});
					}
				}
			})
		},
		methods: {
			stateBtn:function(e){
				if(e.currentTarget.dataset.state==1){
					this.state=1
				}else{
					this.state=2
				}
				uni.showLoading({
				    title: '加载中'
				});
				http.get('/api/purchase/productList',{buyType:this.state==1?0:1}).then(res => {
					if (res.code == 200 && res.result) {
						this.list=res.result
						uni.hideLoading();
						if(!res.result.length){
							uni.showToast({
							  title: '暂无数据',
							  icon: 'none'
							});
						}
					}
				})
			},
			xdNavBtn:function(e){
				if(e.currentTarget.dataset.state==1){
					this.xdNavC=1
				}else{
					this.xdNavC=2
				}
				this.num=null
			},
			gmBtn:function(e){
				this.xdBol=true
				uni.showLoading({
				    title: '加载中'
				});
				http.get('/api/purchase/getMyProduct',{productId:e.currentTarget.dataset.id}).then(res => {
					//console.log(res)
					if (res.code == 200) {
						this.gmInfo=res.result
					}
					uni.hideLoading();
				})
			},
			qxBtn:function(){
				this.xdBol=false
				this.num=null
			},
			qbBtn:function(){
				if(this.xdNavC==1){
					this.num=this.gmInfo.maxAmount
				}else{
					this.num=this.gmInfo.pnumber
					//this.num1=toFixed(num*gmInfo.amount)
				}
			},
			orderBtn:function(){
				let that=this
				if(!this.num){
					uni.showToast({
					  title: '请填写金额或数量购买',
					  icon: 'none'
					});
					this.xdBol=false
					return false
				}
				var order={}
				// var amount=0
				// var number=0
				if(this.xdNavC==1){
					//amount=this.num
					order.amount=this.num
					order.productId=this.gmInfo.id
				}else{
					order.number=this.num
					order.productId=this.gmInfo.id
				}
				uni.showLoading({
				    title: '加载中'
				});
				http.post('/api/purchase/buyProduct',order).then(res => {
					//console.log(res)
					if (res.code == 200 && res.result) {
						uni.navigateTo({
							url:'/pages/order/order?tradeId='+res.result.tradeId
						})
					}
				})
				uni.hideLoading();
				that.xdBol=false
			}
		}
	}
</script>

<style>
	.nav{
		display: flex;
		flex-direction: row;
		color: #999999;
		align-items: flex-end;
		margin-bottom: 2vw;
	}
	.state{
		font-size: 5vw;
		font-weight: bold;
		color: #007AFF;
		border-bottom: 1px solid #007AFF;
	}
	.nav>view{
		padding: 2vw 6vw;
		margin-left: 6vw;
	}
	.list{
		padding: 2vw 6vw;
		display: flex;
		flex-direction: column;
		border-bottom: 1px solid #F2F2F2;
	}
	.list>view:nth-child(1),.list>view:nth-child(2),.list>view:nth-child(3){
		font-size: 3.8vw;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: flex-end;
	}
	.list>view:nth-child(1)>view:nth-child(1) span{
		color: #007AFF;
		margin-left: 2vw;
	}
	.list>view:nth-child(1){
		margin-bottom: 4vw;
	}
	.list>view:nth-child(2),.list>view:nth-child(3){
		color: #999999;
		font-size: 3.2vw;
	}
	.czBtn{
		font-size: 3.2vw;
		color: #999999;
	}
	.num{
		font-size: 5vw;
		color: #007AFF;
		font-weight: bold;
	}
	.num span{
		font-size: 3.2vw;
	}
	.gmBtn1{
		display: flex;
		justify-content: flex-end;
		margin-top: 2vw;
	}
	.gmBtn{
		text-align: center;
		width: 22vw;
		border-radius: 1vw;
		padding: 1.5vw 0;
		background: #007AFF;
		color: #FFFFFF;
		font-size: 3.8vw;
	}
	.xd{
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		background: rgba(0,0,0,0.3);
		z-index: 9999;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
	}
	.xdMain{
		width: 100%;
		background: #F2F2F2;
		border-radius: 2vw 2vw 0 0;
	}
	.xdMain>view:nth-child(1){
		padding: 2vw;
		font-size: 3.8vw;
	}
	.xdMain>view:nth-child(1)>view:nth-child(2){
		color: #007AFF;
	}
	.xdMain>view:nth-child(2){
		background: #FFFFFF;
	}
	.xdNav{
		display: flex;
		flex-direction: row;
		padding: 2vw;
		font-size: 3.8vw;
		color: #999999;
	}
	.xdNav>view{
		padding: 2vw;
	}
	.xdNavC{
		color: #007AFF;
		border-bottom: 1px solid #007AFF;
	}
	.xdInp{
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 2.2vw;
		margin: 2vw;
		border: 1px solid #F2F2F2;
		border-radius: 1vw;
		font-size: 3.8vw;
	}
	.xdInp>view{
		width: 25%;
		text-align: center;
		color: #007AFF;
	}
	.xdInp>input{
		flex: 1;
		font-size: 3.8vw;
		height: 8vw;
		line-height: 8vw;
	}
	.xdTxt{
		padding: 0.2vw 2vw;
		font-size: 3.2vw;
		color: #666666;
	}
	.xdTxt>span{
		color: #007AFF;
		font-size: 5vw;
	}
	.xdTxt>span>span{
		font-size: 3.2vw;
	}
	.btnList{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 2%;
	}
	.btnList>view{
		width: 48%;
		height: 10vw;
		line-height: 10vw;
		text-align: center;
		font-size: 3.7vw;
		color: #FFFFFF;
		border-radius: 1vw;
	}
	.btnList>view:nth-child(1){
		background: #999999;
	}
	.btnList>view:nth-child(2){
		background: #007AFF;
	}
</style>
